/**
 * COPYRIGHT HangZhou Trustway Technology Company Limited
 * All right reserved.
 */
package com.robam.cloud.component.componentfeign.initdata;

import com.robam.cloud.common.bean.apicenter.entity.SysDictType;
import com.robam.cloud.common.bean.apicenter.entity.SysDictValue;
import com.robam.cloud.common.bean.restful.entity.RespMsg;
import com.robam.cloud.common.bean.restful.entity.ServiceException;
import com.robam.cloud.component.componentfeign.basefeign.FeignApiCenterBaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * @Description
 * @Author 薛铁琪
 * @CreateTime 2020-07-28 15:01
 * @Version 1.0
 */
@Component
public class SysDictData {

    @Autowired
    FeignApiCenterBaseUtil feignApiCenterBaseUtil;

    private HashMap<String, List<SysDictValue>> stringListHashMap;


    private void init() {
        if (stringListHashMap == null || stringListHashMap.size() <= 0) {
            //加载stringListHashMap
            RespMsg respMsg = feignApiCenterBaseUtil.getAllSysDictType();
            if (respMsg == null) {
                throw new ServiceException("api中心异常请稍后重试");
            }
            if (respMsg.getCode() != 200) {
                throw new ServiceException(respMsg.getMsg());
            }
            List<SysDictType> sysDictTypes = (List<SysDictType>) respMsg.getData();
            stringListHashMap = new HashMap<>();
            if (sysDictTypes != null && sysDictTypes.size() > 0) {
                for (SysDictType sysDictType : sysDictTypes) {
                    respMsg = feignApiCenterBaseUtil.getSysDictValueByType(sysDictType.getDmlb());
                    if (respMsg == null) {
                        throw new ServiceException("api中心异常请稍后重试");
                    }
                    if (respMsg.getCode() != 200) {
                        throw new ServiceException(respMsg.getMsg());
                    }
                    List<SysDictValue> sysDictValues = (List<SysDictValue>) respMsg.getData();
                    stringListHashMap.put(sysDictType.getDmlb(), sysDictValues);
                }
            }
        }
    }

    public List<SysDictValue> getSysDicValueByDmlb(String dmlb) {
        init();
        return stringListHashMap.get(dmlb);
    }

    public String getSysDicValueByDmlbAndDmbh(String dmlb, String dmbh) {
        init();
        List<SysDictValue> sysDictValues = stringListHashMap.get(dmlb);
        for (SysDictValue sysDictValue : sysDictValues) {
            if (sysDictValue.getDmbh().equals(dmbh)) {
                return sysDictValue.getDmmc();
            }
        }
        return null;
    }
}
